package com.wgchao.authmanager.application.controller;

import com.wgchao.authmanager.application.filter.AuthMethod;
import com.wgchao.authmanager.application.filter.Authentication;
import com.wgchao.authmanager.application.service.AdministratorService;
import com.wgchao.authmanager.application.vo.AdministratorVo;
import com.wgchao.authmanager.application.vo.RoleVo;
import com.wgchao.authmanager.domain.params.AdministratorCreator;
import com.wgchao.authmanager.domain.params.AdministratorUpdator;
import com.wgchao.authmanager.infrastructure.resp.BackResult;
import com.wgchao.authmanager.infrastructure.resp.PageResult;
import com.wgchao.authmanager.query.RoleInfoQuery;
import com.wgchao.authmanager.query.UserInfoQuery;
import com.wgchao.authmanager.query.specification.UserInfoCriteria;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @作者：wgchao
 * @时间：21:49 2020/5/5 0005
 * @用途：账户管理
 **/
@RestController
@RequestMapping("/aum/front/admin")
@Api(value = "/aum/front/admin", description = "账户管理")
public class AdministratorManagerController {
    @Autowired
    private AdministratorService administratorService;
    @Autowired
    private UserInfoQuery userInfoQuery;
    @Autowired
    private RoleInfoQuery roleInfoQuery;

    @PostMapping("/{agencyCode}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "向机构添加账户", notes = "需要身份验证")
    public BackResult createAdminToAgency(@ApiParam(name = "agencyCode", required = true, value = "机构编号（url参数）")
                                              @PathVariable String agencyCode,
                                          @ApiParam(name = "creator", required = true, value = "账户信息")
                                          @RequestBody AdministratorCreator creator,
                                          HttpSession session){
        administratorService.addAdmin(agencyCode, creator, session.getAttribute("uname").toString());
        return BackResult.success();
    }

    @PutMapping("/{uname}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "修改账户信息", notes = "需要身份验证")
    public BackResult resiveAdminInfo(@ApiParam(value = "账户名", name = "uname", required = true)
                                      @PathVariable String uname,
                                      @ApiParam(value = "账户修改信息", name = "updator", required = true)
                                      @RequestBody AdministratorUpdator updator,
                                      HttpSession session){
        administratorService.resiveAdminInfo(uname, updator, session.getAttribute("uname").toString());
        return BackResult.success();
    }

    @DeleteMapping("/{uname}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "删除账户", notes = "需要身份验证")
    public BackResult removeAdmin(@ApiParam(value = "账户名", name = "uname", required = true)
                                  @PathVariable String uname,
                                  HttpSession session){
        administratorService.removeAdmin(uname, session.getAttribute("uname").toString());
        return BackResult.success();
    }

    @PutMapping("/stop/{uname}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "停用账户", notes = "需要身份验证")
    public BackResult stopUsingAdmin(@ApiParam(value = "账户名", name = "uname", required = true)
                                     @PathVariable String uname,
                                     HttpSession session){
        administratorService.stopUsingAdmin(uname, session.getAttribute("uname").toString());
        return BackResult.success();
    }

    @PutMapping("/reuse/{uname}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "重新启用管理员账户", notes = "需要身份验证")
    public BackResult reUseAdmin(@ApiParam(value = "账户名", name = "uname", required = true)
                                     @PathVariable String uname,
                                 HttpSession session){
        administratorService.reUseAdmin(uname, session.getAttribute("uname").toString());
        return BackResult.success();
    }

    @GetMapping("/page")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "条件分页查询", notes = "需要身份验证")
    public BackResult<PageResult<AdministratorVo>> searchAdminsOfCri(UserInfoCriteria criteria) throws Exception{
        PageResult<AdministratorVo> administratorVoPageResult = userInfoQuery.searchAdminsOfCri(criteria, "admin");
        return BackResult.success().setData(administratorVoPageResult);
    }

    @GetMapping("/one/{uname}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "查询账户信息", notes = "需要身份验证")
    public BackResult<AdministratorVo> searchAdminInfo(@ApiParam(value = "账户名", name = "uname", required = true)
                                                           @PathVariable String uname){
        AdministratorVo administratorVo = userInfoQuery.searchAdminInfo(uname);
        return BackResult.success().setData(administratorVo);
    }

    @PostMapping("/role/{uname}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "向账户设置角色", notes = "需要身份验证")
    public BackResult setRolesToAdmin(@ApiParam(value = "账户名", name = "uname", required = true)
                                      @PathVariable String uname,
                                      @ApiParam(value = "角色名列表", name = "roleNames", required = true)
                                      @RequestBody List<String> roleNames,
                                      HttpSession session){
        administratorService.setRolesToAdmin(uname, roleNames, session.getAttribute("uname").toString());
        return BackResult.success();
    }


    @GetMapping("/search/role/{uname}")
    @Authentication(type = AuthMethod.NORMAL)
    @ApiOperation(value = "查询用户拥有角色", notes = "需要身份验证")
    public BackResult<List<RoleVo>> searchRolesOfAdmin(@ApiParam(value = "账户名", name = "uname", required = true)
                                                       @PathVariable String uname){
        List<RoleVo> roleVos = roleInfoQuery.searchRolesOfAdmin(uname);
        return BackResult.success().setData(roleVos);
    }
}
